package com.feemanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.annotation.Anonymous;
import com.common.core.domain.R;
import com.common.core.page.PageDomain;
import com.common.core.page.TableDataInfo;
import com.common.utils.StringUtils;
import com.common.utils.poi.ExcelUtil;
import com.common.validate.add;
import com.common.validate.edit;
import com.feemanage.domain.BizProjectPayout;
import com.feemanage.domain.BudgetSubjectPayout;
import com.feemanage.domain.bo.PayoutDetailsBo;
import com.feemanage.domain.bo.ProjectPayoutBo;
import com.feemanage.domain.vo.BizProjectPayoutVo;
import com.feemanage.domain.vo.ProjectPayoutVo;
import com.feemanage.service.IBizProjectPayoutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;

/**
 * 经费支出
 */
@Api(tags="经费支出")
@RestController
@RequestMapping("/feemanage/payout")
public class BizProjectPayoutController {

    @Autowired
    private IBizProjectPayoutService baseService;

    /**
     * 查询列表
     * @param pageDomain
     * @param payout
     * @return
     */
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public TableDataInfo list(PageDomain pageDomain, BizProjectPayout payout){
        IPage<BizProjectPayoutVo> page = baseService.selectList(pageDomain,payout);
        return TableDataInfo.build(page.getRecords(),page.getTotal());
    }

    /**
     * 导出经费支出列表
     */
    @ApiOperation("导出经费支出列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody PageDomain pageDomain, @RequestBody BizProjectPayout payout)
    {
        List <BizProjectPayoutVo> list = baseService.selectExportList(pageDomain,payout);
        ExcelUtil<BizProjectPayoutVo> util = new ExcelUtil<>(BizProjectPayoutVo.class);
        util.exportExcel(response, list, "经费支出列表");
    }

    /**
     * 根据项目id获取预算信息
     * @param detailsBo ,传projectid和feesource
     * @return
     */
    @ApiOperation("根据项目id获取预算信息，必传projectId，feesource，projectClass")
    @GetMapping("/getpayoutInfo")
    public R<List<BudgetSubjectPayout>> getpayoutInfo(PayoutDetailsBo detailsBo){
        return R.ok(baseService.getpayoutInfo(detailsBo));
    }

    /**
     * 获取编辑/查看数据
     * @param payoutBo
     * @return
     */
    @ApiOperation("获取页面展示信息,编辑/查看时用,必传主表id对应字段--id")
    @GetMapping("/getEditInfo")
    public R<ProjectPayoutVo> getEditInfo(ProjectPayoutBo payoutBo){
        return R.ok(baseService.getEditInfo(payoutBo));
    }


    /**
     * 新增
     * @param payoutBo
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/add")
    public R<Void> addProOutFee(@Validated(add.class) @RequestBody ProjectPayoutBo payoutBo){
        String msg = baseService.addProOutFee(payoutBo);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 修改
     * @param payoutBo
     * @return
     */
    @ApiOperation("编辑")
    @PostMapping("/edit")
    public R<Void> editProOutFee(@Validated(edit.class) @RequestBody  ProjectPayoutBo payoutBo){
        String msg = baseService.editProOutFee(payoutBo);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove( @NotEmpty(message = "主键不能为空") @PathVariable String[] ids)
    {
        boolean result = baseService.delProOutFee(Arrays.asList(ids),true);
        return result ? R.ok() : R.fail();
    }

}
